//
//  SettingViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "SettingViewController.h"
#import "ItemModel.h"
#import "GroupModel.h"
#import "ItemSwitchModel.h"
#import "ItemArrowModel.h"
#import "SettingViewController.h"
#import "SettingCell.h"
#import "pictureQualitiController.h"
#import "ItemLogoutModel.h"



@interface SettingViewController ()

@end

@implementation SettingViewController

-(NSArray *)setData{
    
    //.设置数据模型
    //第一组
    ItemModel *item11 = [ItemArrowModel itemWithTitle:@"账号管理" ];
//    item1.subTitle = @"(100)";
    ItemModel *item12 = [ItemArrowModel itemWithTitle:@"账号安全"];
    GroupModel *group1 = [GroupModel groupWithItems:@[item11,item12]];
    
    //第二组
    ItemModel *item21 = [ItemArrowModel itemWithTitle:@"通知" ];
    ItemModel *item22 = [ItemArrowModel itemWithTitle:@"隐私"];
    ItemModel *item23 = [ItemArrowModel itemWithTitle:@"通用设置"];
    GroupModel *group2 = [GroupModel groupWithItems:@[item21,item22,item23]];
    
    //第三组
    ItemModel *item31 = [ItemArrowModel itemWithTitle:@"清理缓存" ItemArrowOption:^{
        
        NSFileManager *manager = [NSFileManager defaultManager];
        
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        
        NSString *filePath = [path stringByAppendingPathComponent:@"statues.sqlite"];
        
        BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在
        if(isExist){
            BOOL success = [manager removeItemAtPath:filePath error:nil];
            if(success){
                NSLog(@"清理成功");
            }else{
                //删除失败
                NSLog(@"无缓存文件");
            }
        }
    }];
    ItemModel *item32 = [ItemArrowModel itemWithTitle:@"图片质量" destiController:[pictureQualitiController class]];
    ItemModel *item33 = [ItemSwitchModel itemWithTitle:@"关于微博" ];
    GroupModel *group3 = [GroupModel groupWithItems:@[item31,item32,item33]];
    
    //第四组
//    __weak typeof (self) weakSelf;错误写法
    __weak typeof(self) weakSelf = self;
    ItemModel *itemLogout = [ItemLogoutModel itemWithTitle:@"退出当前账号" option:^{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定退出当前账号吗？" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:sure];
        [alert addAction:cancel];
        
        [weakSelf.navigationController presentViewController:alert animated:YES completion:nil];
        
    }];
    GroupModel *group4 = [GroupModel groupWithItems:@[itemLogout]];
    
    NSArray *dataArray = @[group1,group2,group3,group4];
    
    return dataArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //setNav
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem setNavItemWithTitle:@"返回" target:self action:@selector(back)];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}


//由于点击时的操作不痛，所以子类重写
#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GroupModel *group = self.dataArray[indexPath.section];
    
    ItemModel *item = group.items[indexPath.row];
    
    if ([item isKindOfClass:[ItemArrowModel class]]) {
        
        ItemArrowModel *itemArrow = (ItemArrowModel *)item;
        
        if (itemArrow.destiController) {
            
            UIViewController *destController = [[itemArrow.destiController alloc]init];
            
            [self.navigationController pushViewController:destController animated:YES];
        }else if (itemArrow.option){
            itemArrow.option();
        }
    }else if([item isKindOfClass:[ItemLogoutModel class] ] ){
        
        ItemLogoutModel *itemLogout = (ItemLogoutModel *)item;
        
        if (itemLogout.option) {
            
            itemLogout.option();
            
        }
    }
    //点击后立刻取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
